package com.apac.controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jmesa.limit.ExportType;
import org.jmesa.model.TableModel;
import org.jmesa.view.component.Column;
import org.jmesa.view.component.Row;
import org.jmesa.view.component.Table;
import org.jmesa.view.html.component.HtmlColumn;
import org.jmesa.view.html.component.HtmlRow;
import org.jmesa.view.html.component.HtmlTable;
import org.jmesa.view.html.event.RowEvent;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.apac.bom.TestObject;
import com.apac.dao.JmesaColumnsDAO;
import com.apac.dao.TestDao;
import com.apac.jmesa.JmesaTableColumnNames;
import com.apac.jmesa.JmesaEvents;
import com.apac.jmesa.JmesaObjectList;
import com.apac.jmesa.RowEventJmesa;
import com.apac.services.TableModelGeneratorService;

@Controller
@RequestMapping("/contracts")
public class ContractsReleaseController {

	@RequestMapping(value = "", method = RequestMethod.GET)
	public ModelAndView contractsRelease(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		ModelAndView mv = new ModelAndView("ContractsRelease");
		String contracts = generateContracts(request, response);
		String summary = generateSummary(request, response);
		mv.addObject("summary", summary);
		mv.addObject("contracts", contracts);
		return mv;
	}

	@RequestMapping(value = "/all", method = RequestMethod.GET)
	public ModelAndView contractsReleaseContracts(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String contracts = generateContracts(request, response);
		return null;
	}

	@RequestMapping(value = "/summary", method = RequestMethod.GET)
	public ModelAndView contractsReleaseSummary(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		System.out.println("PUMASOK");
		String summary = generateSummary(request, response);
		
		return null;
	}
	
	public String generateSummary(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		/** MOCK DATA **/
		ArrayList<TestObject> data = new ArrayList<TestObject>();
		TestDao tDao = new TestDao();
		data = tDao.retrieveAll();
		/** MOCK DATA **/

		TableModel tableModel = new TableModel("summary", request, response);

		tableModel.setExportTypes(ExportType.CSV, ExportType.JEXCEL,
				ExportType.PDFP);
		tableModel.setItems(data);

		if (tableModel.isExporting()) {
			Table Table = new Table()
					.caption("Contracts Release Summary Record");

			Row Row = new Row();
			Table.setRow(Row);

			Column col1 = new Column("att1").title("Account");
			Row.addColumn(col1);

			Column col2 = new Column("att1").title("For Notarization");
			Row.addColumn(col2);

			Column col3 = new Column("att2").title("Released");
			Row.addColumn(col3);

			Column col4 = new Column("att3").title("(Blank)");
			Row.addColumn(col4);

			Column col5 = new Column("att1").title("Total");
			Row.addColumn(col5);

			tableModel.setTable(Table);

			String export = tableModel.render();
			// request.setAttribute("summary", export);
			// String ajax = request.getParameter("ajax");
			// if (ajax != null && ajax.equals("true")) {
			// byte[] contents = export.getBytes();
			// response.getOutputStream().write(contents);
			// return null;
			// }
			return export;
		} else {
			HtmlTable htmlTable = new HtmlTable();

			HtmlRow HtmlRow = new HtmlRow();
			htmlTable.setRow(HtmlRow);

			HtmlRow.setOnclick(new RowEvent() {
				public String execute(Object item, int rowcount) {
					return "selectRow(this," + ((TestObject) item).getId()
							+ ",'summaryId')";
				}
			});

			HtmlColumn col1 = new HtmlColumn("id").title("Account");
			HtmlRow.addColumn(col1);

			HtmlColumn col2 = new HtmlColumn("att1").title("For Notarization");
			HtmlRow.addColumn(col2);

			HtmlColumn col3 = new HtmlColumn("att2").title("Released");
			HtmlRow.addColumn(col3);

			HtmlColumn col4 = new HtmlColumn("att3").title("(Blank)");
			HtmlRow.addColumn(col4);

			HtmlColumn col5 = new HtmlColumn("att1").title("Total");
			HtmlRow.addColumn(col5);

			// HtmlColumn viewLink = new HtmlColumn("id").title("View");
			// viewLink.setCellEditor(new CellEditor() {
			// public Object getValue(Object item, String property, int
			// rowcount) {
			// //Object value = new HtmlCellEditor().getValue(item, property,
			// rowcount);
			// HtmlBuilder html = new HtmlBuilder();
			// ExamResponse er = (ExamResponse)item;
			// html.a().href().quote().append("JMesaExportStudentExamServlet?id="+er.getExamResponseID()).quote().close();
			// html.append("View Response");
			// html.aEnd();
			// return html.toString();
			// }
			// });
			// viewLink.setSortable(false);
			// viewLink.setFilterable(false);
			// htmlRow.addColumn(viewLink);

			tableModel.setTable(htmlTable);

			String html = tableModel.render();
			request.setAttribute("summary", html);
			String ajax = request.getParameter("ajax");
			if (ajax != null && ajax.equals("true")) {
				byte[] contents = html.getBytes();
				response.getOutputStream().write(contents);
				return null;
			}
			return html;
		}

	}

	public String generateContracts(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		JmesaColumnsDAO jmColsDao = new JmesaColumnsDAO();
		List<String> keys = jmColsDao
				.findByTableId(JmesaTableColumnNames.CONTRACTS_RELEASE);

		
		JmesaObjectList items = new JmesaObjectList(keys);
		
		for(int i=0; i<5; i++){
			TreeMap<String, Object> sampleMap = new TreeMap<String, Object>();
			for(String key: keys){
				sampleMap.put(key, key.concat(" "+i));
			}		
			items.add(sampleMap);	
		}
		
		JmesaEvents reJ= new JmesaEvents();
		RowEventJmesa reJmesa = new RowEventJmesa("contractsId", "Contract ID",reJ);
		String table = TableModelGeneratorService.generateTable("contracts",
				request, response, items, reJmesa);

		request.setAttribute("contracts", table);
		String ajax = request.getParameter("ajax");
		if (ajax != null && ajax.equals("true")) {
			byte[] contents = table.getBytes();
			response.getOutputStream().write(contents);
			return null;
		}
		return table;

	}

}
